package com.bubbles.workspace.sync.entity.source.metadata;


import com.bubbles.engine.data.core.entity.BaseEntity;
import com.bubbles.workspace.sync.key.source.metadata.SourceServerKey;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>description: 源数据源-服务器 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-19
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true, exclude = {"sourceJdbcs"})
@ToString(callSuper = true, exclude = {"sourceJdbcs"})
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(schema = "sync", name = "source_server")
@IdClass(SourceServerKey.class)
@Comment("源数据源-服务器")
public class SourceServer extends BaseEntity {
    
    @Id
    @Comment("服务器-主机")
    private String host;
    
    @Id
    @Comment("服务器-端口")
    private Integer port;
    
    @Comment("服务器-可连接")
    private Boolean connected;
    
    @Comment("服务器-最后可连接时间")
    private Date connectedDatetime;
    
    /**
     * 关联关系
     */
    
    @OneToMany(mappedBy = "sourceServer", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private List<SourceJdbc> sourceJdbcs = new ArrayList<>();
    
    /**
     * 其他
     */
    
    public SourceServerKey getKey() {
        return new SourceServerKey().setHost(this.host)
                                    .setPort(this.port);
    }
    
    public String getStringKey() {
        return getKey().toString();
    }
    
    public String getInetAddress() {
        return this.host + ":" + this.port;
    }
}
